package sdk;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一返回对象 数据格式
 *
 * @author 王松
 * @WX-QQ 1720696548
 * @date 2019/11/14 14:55
 */
@Data
public class XjHttpRequestResult implements Serializable {

    private static final long serialVersionUID = -5666504070515657048L;

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 状态描叙
     */
    private String msg;

    /**
     * 返回数据
     */
    private Object data;

    /**
     * 错误的栈内存信息  注解:空不返回
     */
    @JsonInclude(value = JsonInclude.Include.NON_EMPTY)
    private String errorMsg;

    /**
     * 是否请求成功
     */
    private boolean isOk;

    /**
     * code，msg方式
     *
     * @param code     code
     * @param msg      msg
     * @param data     data
     * @param errorMsg errorMsg
     * @author ws
     * @email 1720696548@qq.com
     * @date 2020/4/19 0019 14:55
     */
    public XjHttpRequestResult(Integer code, String msg, Object data, String errorMsg, boolean isOk) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        this.errorMsg = errorMsg;
        this.isOk = isOk;
    }

    public static XjHttpRequestResult success(Object data) {
        return new XjHttpRequestResult(200, "ok", data, null, true);
    }

    public static XjHttpRequestResult fail(Integer code, String errorMsg) {
        return new XjHttpRequestResult(code, "fail", null, errorMsg, false);
    }
}
